package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.SysPrivilege;
import com.ynsj.model.R;
import com.ynsj.service.SysPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/privileges")
@Api(tags = "权限管理")
public class SysPrivilegeController {

    @Autowired
    private SysPrivilegeService privilegeService;


    /**
     * 查询权限列表-分页
     * @param privilegePage
     * @return
     */
    @GetMapping
    @ApiOperation("查询权限列表-分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示的大小")
    })
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public R<Page<SysPrivilege>> findByPage(@ApiIgnore Page<SysPrivilege> privilegePage){
        return R.ok(privilegeService.findPrivilegePage(privilegePage));
    }

    /**
     * 新增权限
     * @param sysPrivilege
     * @return
     */
    @PostMapping
    @ApiOperation("新增权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysPrivilege",value = "sysPrivilege数据对象")
    })
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    public R add(@RequestBody @Validated SysPrivilege sysPrivilege){
        boolean save = privilegeService.addPrivilege(sysPrivilege);
        if(save){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    /**
     * 修改权限
     * @param sysPrivilege
     * @return
     */
    @PatchMapping
    @ApiOperation("修改权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysPrivilege",value = "sysPrivilege数据对象")
    })
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    public R update(@RequestBody @Validated SysPrivilege sysPrivilege){
        boolean update = privilegeService.updatePrivilege(sysPrivilege);
        if(update){
            return R.ok("更新成功");
        }
        return R.fail("更新失败");
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "要删除的权限id")
    })
    @PreAuthorize("hasAuthority('sys_privilege_delete')")
    public R delete(@PathVariable String id){
        boolean delete = privilegeService.deletePrivilege(id);
        if(delete){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

}
